<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="js/vue.js"></script>
    <script src="js/elementUI.js"></script>
    <script src="js/axios.js"></script>
    <link rel="stylesheet" href="css/elementUI.css">
</head>
<body>
<div id="app">
    <el-table
            :data="tableData"
            style="width: 100%">
        <el-table-column
                prop="id"
                label="商品ID"
                width="180">
        </el-table-column>
        <el-table-column
                prop="name"
                label="商品名称"
                width="180">
        </el-table-column>
        <el-table-column
                prop="price"
                label="商品价格"
                width="180">
        </el-table-column>
        <el-table-column
                prop="num"
                label="商品库存"
                width="180">
        </el-table-column>
        <el-table-column
                prop="content"
                label="商品信息"
                width="180">
        </el-table-column>
        <el-table-column
                label="操作">
            <template slot-scope="scope">
                <el-button @click="handleClick(scope.row.id)" type="text" size="small">查看</el-button>
            </template>
        </el-table-column>
    </el-table>
    <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="pageNum"
            :page-sizes="[2, 4, 6, 8]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
    </el-pagination>


    <el-dialog
            title="商品信息"
            :visible.sync="dialogVisible"
            width="30%"
            :before-close="handleClose">
        <div>
            <h1>{{pojo.name}}</h1>
            价格：{{pojo.price}}
            库存：{{pojo.num}} <br>
            商品描述： <br>
            {{pojo.content}}
        </div>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="addOrder" >立即购买</el-button>
        </div>
    </el-dialog>
</div>
<script>
    new Vue({
        el: "#app",
        data: {
            tableData: [],
            pageNum: 1,
            pageSize: 2,
            total: 0,
            dialogVisible: false,
            pojo: {}

        },
        methods: {
            handleSizeChange(val) {
                this.pageSize = val;
                this.findPage();
            },
            handleCurrentChange(val) {
                this.pageNum = val;
                this.findPage();
            },
            findPage() {
                axios.post(`/goods/findPage?pageNum=${this.pageNum}&pageSize=${this.pageSize}`).then(res => {
                    this.tableData = res.data.records;
                    this.total = res.data.total;
                })
            },
            handleClick(id) {
                this.dialogVisible = true;
                axios.get(`/goods/findById?id=${id}`).then(res => {
                    this.pojo = res.data;
                })
            },
            addOrder() {
                let order = {
                    gid: this.pojo.id,
                    num: 1
                };
                axios.post(`/order/add`, order).then(res => {
                    if (res.data == 300) {
                        alert("请登录之后在购买！")
                    }
                    if (res.data == 200) {
                        alert("购买成功！");
                        this.findPage();
                        this.dialogVisible =false;
                    }
                }).catch(e=>{
                    alert("库存异常，终止交易！");
                })
            }
        },
        created() {
            this.findPage();
        }
    })
</script>
</body>
</html>